package com.mm.common.utils.theadPoolUtil;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.HashMap;
import java.util.Map;

/**
 * @Program: 8.mindmap
 * @Author: 海里的鱼
 * @Create: 2024-08-26 09:22
 * @Name: 批量导入任务的抽象类类
 * @Desc: TODO
 */
public abstract class BulkTask {

    //任务名
    protected String name;
    //任务状态
    protected TaskEnum state = TaskEnum.UNEXECUTED;
    //开始时间
    protected LocalDateTime startTime;
    //结束时间
    protected LocalDateTime endTime;




    //执行函数，任务执行失败一定要抛异常
    public abstract boolean executeFunction() throws Exception;

    /** ---------------------------------------------------------------------------------------------------------------------
     * @Method  : 获取运行时间
     * @Describe: TODO
     **/
     public Long getRunningTime(){
        if(startTime == null || endTime == null){
            return -1L;
        }
        return ChronoUnit.MILLIS.between(startTime, endTime);
    }

    /** ---------------------------------------------------------------------------------------------------------------------
     * @Method  : 获取任务的信息
     * @Describe: TODO
     **/
     public Map<String,Object> getTaskInfo(){
        Map<String,Object> info = new HashMap<>();
        info.put("name",name);
        info.put("state", state.getMessage());
        info.put("startTime", this.startTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        info.put("endTime", this.endTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
//        info.put("result", this.result);
        info.put("runningTime", String.valueOf(ChronoUnit.MILLIS.between(startTime, endTime)));
        return info;
    }

    public TaskEnum getState() {
        return state;
    }

    public void setState(TaskEnum state) {
        this.state = state;
    }

    public LocalDateTime getStartTime() {
        return startTime;
    }

    public void setStartTime(LocalDateTime startTime) {
        this.startTime = startTime;
    }

    public LocalDateTime getEndTime() {
        return endTime;
    }

    public void setEndTime(LocalDateTime endTime) {
        this.endTime = endTime;
    }


    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

